import { request } from 'umi';
import getApiData from "@/services/cpasRequest";
import { intl } from "@/utils";
import notify from "devextreme/ui/notify";

/**
 * 获取底稿附件列表
 * @param dbname 数据库名
 * @param dgid 底稿ID，年度+名称
 * @returns []
 */
export async function getAttachmentList(dbname: string, dgid: string) {
  const res = await getApiData('sjdg.fjlist', {
    dbname: dbname,
    dgid: dgid,
  });
  return Promise.resolve({
    data: res.data,
    success: res.ok,
  });
}

/**
 * 新建文件夹
 * @returns 
 */
export async function editFolder(params: {
  dbname: string,
  dgid: string,
  folderName: string,
  folderID?: string,
  username?: string,
  parentID?: string
}): Promise<any> {
  const res = await getApiData('sjdg.editFolder', params);

  return Promise.resolve({
    data: res.data,
    success: res.ok,
  });
}

/**
 * 删除一个目录
 * @param folderID 
 * @returns 
 */
export async function delFolder(params: {
  dbname: string,
  folderID: string,
  username: string
}): Promise<any> {
  const res = await getApiData('sjdg.delFolder', params);
  return Promise.resolve({
    data: res.data,
    success: res.ok,
  });
}
/**
 * 修改 上级目录，拖放时使用
 * @param dbname 
 * @param linkID 
 * @param pLinkID 上级ID，为Null时为顶级 
 * @returns 
 */
export async function dgLinkInfoUpdParent(dbname: string, linkID: string, pLinkID: string): Promise<boolean> {
  const res = await getApiData("sjdg.dgLinkInfoUpdParent", { dbname, linkID, pLinkID });
  if (res.ok) {
    notify(intl('relationDrawer.attach.success', '操作成功'), 'success', 1000);
  } else {
    notify(res.data, 'error');
  }
  return res.ok;
}

export async function dgUnchain(
  dbname: string,
  dgid: string,
  fjid: string
): Promise<any> {
  const res = await request('/cpasApi/draftFjManage/fjUnchain', {
    method: 'POST',
    data: {
      dbname: dbname,
      dgid: dgid,
      fjid: fjid
    },
  });
  return Promise.resolve({
    data: res.data,
    success: res.ok,
  });
}

